package org.springblade.tms.module.currencytype.base;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.logging.log4j.util.Strings;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.tms.global.enums.WhetherEnum;
import org.springblade.tms.global.exception.CommonException;
import org.springblade.tms.module.currencytype.dto.TMCurrencyTypeDTO;
import org.springblade.tms.module.currencytype.entity.TMCurrencyType;
import org.springblade.tms.module.currencytype.service.ICurrencyTypeService;
import org.springblade.tms.module.currencytype.vo.TMCurrencyTypeVO;
import org.springblade.tms.module.currencytype.wrapper.CurrencyTypeWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @projectName: BladeX
 * @package: org.springblade.tms.module.currencytype.base
 * @className: CurrencyService
 * @author: yr
 * @description: 币别服务
 * @date: 2022/4/18 10:24
 */
@Service
public class CurrencyService {
	@Resource
	private ICurrencyTypeService currencyTypeService;

	/**
	 * @param tmCurrencyType:
	 * @param query:
	 * @return R<IPage < TMCurrencyTypeVO>>
	 * @author yr
	 * @description 币种查询
	 * @date 2022/4/18 10:35
	 */
	public R<IPage<TMCurrencyTypeVO>> list(TMCurrencyTypeDTO tmCurrencyType, Query query) {
		IPage<TMCurrencyType> page = currencyTypeService.page(Condition.getPage(query), Condition.getQueryWrapper(new TMCurrencyType()).lambda()
			.like(Strings.isNotBlank(tmCurrencyType.getCurrencyDesc()), TMCurrencyType::getCurrencyDesc, tmCurrencyType.getCurrencyDesc())
			.like(Strings.isNotBlank(tmCurrencyType.getName()), TMCurrencyType::getName, tmCurrencyType.getName()));
		IPage<TMCurrencyTypeVO> currencyTypeVOIPage = CurrencyTypeWrapper.build().pageVO(page);
		for (TMCurrencyTypeVO record : currencyTypeVOIPage.getRecords()) {
			record.setRate(record.getRate().stripTrailingZeros());
			record.setTaxRate(BigDecimal.valueOf(Double.parseDouble(record.getTaxRate().stripTrailingZeros().toPlainString())));
		}
		return R.data(currencyTypeVOIPage);
	}

	/**
	 * @param tmCurrencyType:
	 * @return Boolean
	 * @author yr
	 * @description 新增或修改币种
	 * @date 2022/4/18 11:05
	 */
	public Boolean saveOrUpdate(TMCurrencyType tmCurrencyType) {
		//新增时判断币种是否存在
		tmCurrencyType.setBaseFlag(WhetherEnum.no.value());
		if (tmCurrencyType.getId() == null) {
			List<TMCurrencyType> list = currencyTypeService.lambdaQuery()
				.select(TMCurrencyType::getId)
				.eq(TMCurrencyType::getName, tmCurrencyType.getName()).list();
			if (!list.isEmpty()) {
				throw CommonException.throwCommonException("币种" + tmCurrencyType.getName() + "已存在！");
			}
		}
		return currencyTypeService.saveOrUpdate(tmCurrencyType);
	}

	/**
	 * @param ids:
	 * @return R<Boolean>
	 * @author yr
	 * @description 删除币别
	 * @date 2022/4/18 11:10
	 */
	public R<Boolean> remove(List<Long> ids) {
		TMCurrencyType tmCurrencyType = currencyTypeService.lambdaQuery().in(TMCurrencyType::getId, ids).one();
		//判断删除的是否为人民币
		if (tmCurrencyType.getBaseFlag().equals(WhetherEnum.yes.value())){
			throw CommonException.throwCommonException("人民币不允许删除！");
		}
		boolean deleteLogic = currencyTypeService.deleteLogic(ids);
		return R.status(deleteLogic);
	}
}
